package com.tcs.connection;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.tcs.reptile.accept.Accept;

/**
 * 网络连接
 * 
 * @Title: ProxyPoolConnection.java
 * @Package com.tcs.connection
 * @author 神经刀
 * @date 2018年8月8日
 * @version V1.0
 */
@Component(value = "proxyPoolConnection")
public class ProxyPoolConnection implements IProxyPoolConnection {

	private final Logger logger = LoggerFactory.getLogger(ProxyPoolConnection.class);

	@Resource(name = "pcAccept")
	private Accept accept;

	@Override
	public Document connection(String url, Integer timeOut, int retriesNum) {
		Document document = null;
		Map<String,String> headerMap = null;
		for (int i = 0; i < retriesNum; i++) {
			try {
				headerMap = accept.getAccecptHeader(url);
				logger.info(" headerMap : {} " , headerMap);
				document = Jsoup.connect(url)
//						.headers(headerMap)
						.userAgent(accept.getUserAgent())
						.timeout(timeOut)
						.get();
				break;
			} catch (IOException e) {
				logger.error("" , e);
			}
		}
		return document;
	}
}